package com.gxa.jjty.admin.controller;

import com.gxa.jjty.admin.service.*;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@RestController
@RequestMapping("/healthy")
@Api(tags = "健康管理")
@Log4j2
public class HealthyController {



    @Autowired
    HealthyService healthyService;


    @Autowired
    OprRecordService oprRecordService;

    @Autowired
    VHealthyDService vHealthyDService;

    @PostMapping("/getHealthyd")
    @ApiOperation("查询健康数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "endTime",value = "开始时间"),
            @ApiImplicitParam(name = "startTime",value = "结束时间"),
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "name",value = "残疾人姓名"),
            @ApiImplicitParam(name = "id",value = "残疾人id"),
            @ApiImplicitParam(name = "startScore",value = "开始分数"),
            @ApiImplicitParam(name = "endScore",value = "结束的分数"),
            @ApiImplicitParam(name = "expression",value = "表现")
    })
    public Map selectHealthy( HealthWhere healthWhere){
        List list = healthyService.selectHealthy(healthWhere);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total",list.size());
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }



    @PostMapping("/getDetail")
    @ApiOperation("根据id查询明细")
    public  Map selectByIdHealthy(@RequestParam("handicappedId") String handicappedId){

        VHealthyD vHealthyD = vHealthyDService.selectById(handicappedId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(vHealthyD);
        return jsonResult.getDataMap();
    }

    @PostMapping("/setHealthy")
    @ApiOperation("确定(编辑健康数据)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "handicappedId",value = "残疾人id"),
            @ApiImplicitParam(name = "heartRate",value = "心率"),
            @ApiImplicitParam(name = "bloodGlucose",value = "血糖"),
            @ApiImplicitParam(name = "bloodOxygen",value = "血氧"),
            @ApiImplicitParam(name = "bloodFat",value = "血脂"),
            @ApiImplicitParam(name = "bloodPressure",value = "血压"),
            @ApiImplicitParam(name = "food",value = "膳食"),
    })
    public  Map updateHealthy(@RequestBody Healthy healthy) throws IllegalAccessException, BusinessException {

        healthyService.update(healthy);
        Long handicappedId = healthy.getHandicappedId();
        OprRecord oprRecord = new OprRecord();
        oprRecord.setOpsTime(new Date());
        oprRecord.setHandicappedId(handicappedId);
        oprRecordService.insert(oprRecord);
        healthyService.updateById(healthy);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData("编辑成功");
        return jsonResult.getDataMap();

    }


    @PostMapping("/getOprRecord")
    @ApiOperation("查询操作记录")
    public Map selectHealthy(@RequestParam("id") Long  id){
        OprRecord oprRecord = new OprRecord();
        oprRecord.setHandicappedId(id);
        List<OprRecord> oprList = oprRecordService.select(oprRecord);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(oprList);
        return jsonResult.getDataMap();
    }


}
